<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222"><meta name="generator" content="Hexo 7.3.0">

  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32.ico">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16.ico">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css" integrity="sha256-wiz7ZSCn/btzhjKDQBms9Hx4sSeUYsDrTLg7roPstac=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/fancyapps-ui/5.0.28/fancybox/fancybox.css" integrity="sha256-6cQIC71/iBIYXFK+0RHAvwmjwWzkWd+r7v/BX3/vZDc=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/pace/1.2.4/themes/green/pace-theme-minimal.css">
  <script src="https://cdnjs.cloudflare.com/ajax/libs/pace/1.2.4/pace.min.js" integrity="sha256-gqd7YTjg/BtfqWSwsJOvndl0Bxc8gFImLEkXQT8+qj0=" crossorigin="anonymous"></script>

<script class="next-config" data-name="main" type="application/json">{"hostname":"sumumm.github.io","root":"/","images":"/images","scheme":"Gemini","darkmode":false,"version":"8.19.2","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":{"enable":true,"style":"mac"},"fold":{"enable":true,"height":300},"bookmark":{"enable":false,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":true,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":true,"transition":{"menu_item":"fadeInDown","post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"/search.xml","localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false}}</script><script src="/js/config.js"></script>

    <meta name="description" content="本文主要是Git中分支的新建与合并的相关笔记，若笔记中有错误或者不合适的地方，欢迎批评指正😃。">
<meta property="og:type" content="article">
<meta property="og:title" content="LV01-Git-04-Git本地仓库-06-分支的新建与合并">
<meta property="og:url" content="https://sumumm.github.io/post/6dd28b15.html">
<meta property="og:site_name" content="苏木">
<meta property="og:description" content="本文主要是Git中分支的新建与合并的相关笔记，若笔记中有错误或者不合适的地方，欢迎批评指正😃。">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-04-Git%E6%9C%AC%E5%9C%B0%E4%BB%93%E5%BA%93-06-%E5%88%86%E6%94%AF%E7%9A%84%E6%96%B0%E5%BB%BA%E4%B8%8E%E5%90%88%E5%B9%B6/img/image-20230623094425851.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-04-Git%E6%9C%AC%E5%9C%B0%E4%BB%93%E5%BA%93-06-%E5%88%86%E6%94%AF%E7%9A%84%E6%96%B0%E5%BB%BA%E4%B8%8E%E5%90%88%E5%B9%B6/img/image-20230623094415782.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-04-Git%E6%9C%AC%E5%9C%B0%E4%BB%93%E5%BA%93-06-%E5%88%86%E6%94%AF%E7%9A%84%E6%96%B0%E5%BB%BA%E4%B8%8E%E5%90%88%E5%B9%B6/img/image-20230623094804875.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-04-Git%E6%9C%AC%E5%9C%B0%E4%BB%93%E5%BA%93-06-%E5%88%86%E6%94%AF%E7%9A%84%E6%96%B0%E5%BB%BA%E4%B8%8E%E5%90%88%E5%B9%B6/img/image-20230623094816938.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-04-Git%E6%9C%AC%E5%9C%B0%E4%BB%93%E5%BA%93-06-%E5%88%86%E6%94%AF%E7%9A%84%E6%96%B0%E5%BB%BA%E4%B8%8E%E5%90%88%E5%B9%B6/img/image-20230623095643939.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-04-Git%E6%9C%AC%E5%9C%B0%E4%BB%93%E5%BA%93-06-%E5%88%86%E6%94%AF%E7%9A%84%E6%96%B0%E5%BB%BA%E4%B8%8E%E5%90%88%E5%B9%B6/img/image-20230623095708428.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-04-Git%E6%9C%AC%E5%9C%B0%E4%BB%93%E5%BA%93-06-%E5%88%86%E6%94%AF%E7%9A%84%E6%96%B0%E5%BB%BA%E4%B8%8E%E5%90%88%E5%B9%B6/img/image-20230623100350794.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-04-Git%E6%9C%AC%E5%9C%B0%E4%BB%93%E5%BA%93-06-%E5%88%86%E6%94%AF%E7%9A%84%E6%96%B0%E5%BB%BA%E4%B8%8E%E5%90%88%E5%B9%B6/img/image-20230623100607272.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-04-Git%E6%9C%AC%E5%9C%B0%E4%BB%93%E5%BA%93-06-%E5%88%86%E6%94%AF%E7%9A%84%E6%96%B0%E5%BB%BA%E4%B8%8E%E5%90%88%E5%B9%B6/img/image-20230623100836073.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-04-Git%E6%9C%AC%E5%9C%B0%E4%BB%93%E5%BA%93-06-%E5%88%86%E6%94%AF%E7%9A%84%E6%96%B0%E5%BB%BA%E4%B8%8E%E5%90%88%E5%B9%B6/img/image-20230623100916660.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-04-Git%E6%9C%AC%E5%9C%B0%E4%BB%93%E5%BA%93-06-%E5%88%86%E6%94%AF%E7%9A%84%E6%96%B0%E5%BB%BA%E4%B8%8E%E5%90%88%E5%B9%B6/img/image-20230623101753441.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-04-Git%E6%9C%AC%E5%9C%B0%E4%BB%93%E5%BA%93-06-%E5%88%86%E6%94%AF%E7%9A%84%E6%96%B0%E5%BB%BA%E4%B8%8E%E5%90%88%E5%B9%B6/img/image-20230623101848979.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-04-Git%E6%9C%AC%E5%9C%B0%E4%BB%93%E5%BA%93-06-%E5%88%86%E6%94%AF%E7%9A%84%E6%96%B0%E5%BB%BA%E4%B8%8E%E5%90%88%E5%B9%B6/img/image-20230623102418086.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-04-Git%E6%9C%AC%E5%9C%B0%E4%BB%93%E5%BA%93-06-%E5%88%86%E6%94%AF%E7%9A%84%E6%96%B0%E5%BB%BA%E4%B8%8E%E5%90%88%E5%B9%B6/img/image-20230623102025200.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-04-Git%E6%9C%AC%E5%9C%B0%E4%BB%93%E5%BA%93-06-%E5%88%86%E6%94%AF%E7%9A%84%E6%96%B0%E5%BB%BA%E4%B8%8E%E5%90%88%E5%B9%B6/img/image-20230623102127913.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-04-Git%E6%9C%AC%E5%9C%B0%E4%BB%93%E5%BA%93-06-%E5%88%86%E6%94%AF%E7%9A%84%E6%96%B0%E5%BB%BA%E4%B8%8E%E5%90%88%E5%B9%B6/img/image-20230623102951487.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-04-Git%E6%9C%AC%E5%9C%B0%E4%BB%93%E5%BA%93-06-%E5%88%86%E6%94%AF%E7%9A%84%E6%96%B0%E5%BB%BA%E4%B8%8E%E5%90%88%E5%B9%B6/img/image-20230623103021721.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-04-Git%E6%9C%AC%E5%9C%B0%E4%BB%93%E5%BA%93-06-%E5%88%86%E6%94%AF%E7%9A%84%E6%96%B0%E5%BB%BA%E4%B8%8E%E5%90%88%E5%B9%B6/img/image-20230623103542761.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-04-Git%E6%9C%AC%E5%9C%B0%E4%BB%93%E5%BA%93-06-%E5%88%86%E6%94%AF%E7%9A%84%E6%96%B0%E5%BB%BA%E4%B8%8E%E5%90%88%E5%B9%B6/img/image-20230623103725644.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-04-Git%E6%9C%AC%E5%9C%B0%E4%BB%93%E5%BA%93-06-%E5%88%86%E6%94%AF%E7%9A%84%E6%96%B0%E5%BB%BA%E4%B8%8E%E5%90%88%E5%B9%B6/img/image-20230623103911519.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-04-Git%E6%9C%AC%E5%9C%B0%E4%BB%93%E5%BA%93-06-%E5%88%86%E6%94%AF%E7%9A%84%E6%96%B0%E5%BB%BA%E4%B8%8E%E5%90%88%E5%B9%B6/img/image-20230623104336434.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-04-Git%E6%9C%AC%E5%9C%B0%E4%BB%93%E5%BA%93-06-%E5%88%86%E6%94%AF%E7%9A%84%E6%96%B0%E5%BB%BA%E4%B8%8E%E5%90%88%E5%B9%B6/img/image-20230623104449722.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-04-Git%E6%9C%AC%E5%9C%B0%E4%BB%93%E5%BA%93-06-%E5%88%86%E6%94%AF%E7%9A%84%E6%96%B0%E5%BB%BA%E4%B8%8E%E5%90%88%E5%B9%B6/img/image-20230623104901587.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-04-Git%E6%9C%AC%E5%9C%B0%E4%BB%93%E5%BA%93-06-%E5%88%86%E6%94%AF%E7%9A%84%E6%96%B0%E5%BB%BA%E4%B8%8E%E5%90%88%E5%B9%B6/img/image-20230623104955077.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-04-Git%E6%9C%AC%E5%9C%B0%E4%BB%93%E5%BA%93-06-%E5%88%86%E6%94%AF%E7%9A%84%E6%96%B0%E5%BB%BA%E4%B8%8E%E5%90%88%E5%B9%B6/img/image-20230623110250694.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-04-Git%E6%9C%AC%E5%9C%B0%E4%BB%93%E5%BA%93-06-%E5%88%86%E6%94%AF%E7%9A%84%E6%96%B0%E5%BB%BA%E4%B8%8E%E5%90%88%E5%B9%B6/img/image-20230623110814490.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-04-Git%E6%9C%AC%E5%9C%B0%E4%BB%93%E5%BA%93-06-%E5%88%86%E6%94%AF%E7%9A%84%E6%96%B0%E5%BB%BA%E4%B8%8E%E5%90%88%E5%B9%B6/img/image-20230623111340721.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-04-Git%E6%9C%AC%E5%9C%B0%E4%BB%93%E5%BA%93-06-%E5%88%86%E6%94%AF%E7%9A%84%E6%96%B0%E5%BB%BA%E4%B8%8E%E5%90%88%E5%B9%B6/img/image-20230623112122799.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-04-Git%E6%9C%AC%E5%9C%B0%E4%BB%93%E5%BA%93-06-%E5%88%86%E6%94%AF%E7%9A%84%E6%96%B0%E5%BB%BA%E4%B8%8E%E5%90%88%E5%B9%B6/img/image-20230623112318972.png">
<meta property="article:published_time" content="2023-07-02T07:14:24.000Z">
<meta property="article:modified_time" content="2025-06-13T16:25:57.069Z">
<meta property="article:author" content="苏木">
<meta property="article:tag" content="Git">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-04-Git%E6%9C%AC%E5%9C%B0%E4%BB%93%E5%BA%93-06-%E5%88%86%E6%94%AF%E7%9A%84%E6%96%B0%E5%BB%BA%E4%B8%8E%E5%90%88%E5%B9%B6/img/image-20230623094425851.png">


<link rel="canonical" href="https://sumumm.github.io/post/6dd28b15.html">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"https://sumumm.github.io/post/6dd28b15.html","path":"post/6dd28b15.html","title":"LV01-Git-04-Git本地仓库-06-分支的新建与合并"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>LV01-Git-04-Git本地仓库-06-分支的新建与合并 | 苏木</title>
  








    <script src="/js/browser_tools_disable.js"></script>

  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
<!-- hexo injector head_end start --><link rel="stylesheet" href="https://unpkg.com/hexo-next-tags-plus@latest/lib/tag_plus.css" media="defer" onload="this.media='all'"><!-- hexo injector head_end end --></head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <div class="column">
      <header class="header" itemscope itemtype="http://schema.org/WPHeader"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <p class="site-title">苏木</p>
      <i class="logo-line"></i>
    </a>
      <p class="site-subtitle" itemprop="description">我的学习之路</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger" aria-label="搜索" role="button">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu"><li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>苏木的家</a></li><li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类页<span class="badge">42</span></a></li><li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档页<span class="badge">673</span></a></li><li class="menu-item menu-item-flink"><a href="/flink/" rel="section"><i class="fa fa-link fa-fw"></i>友人帐</a></li><li class="menu-item menu-item-about"><a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于我</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup"><div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off" maxlength="80"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close" role="button">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="search-result-container no-result">
  <div class="search-result-icon">
    <i class="fa fa-spinner fa-pulse fa-5x"></i>
  </div>
</div>

    </div>
  </div>

</header>
        
  
  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%B8%80%E3%80%81%E5%87%86%E5%A4%87%E5%B7%A5%E4%BD%9C"><span class="nav-text">一、准备工作</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-%E5%B7%A5%E4%BD%9C%E5%9C%BA%E6%99%AF"><span class="nav-text">1. 工作场景</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-%E6%96%87%E4%BB%B6%E5%87%86%E5%A4%87"><span class="nav-text">2. 文件准备</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%BA%8C%E3%80%81BUG%E5%A4%84%E7%90%86"><span class="nav-text">二、BUG处理</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-%E6%83%85%E5%86%B5%E6%8F%8F%E8%BF%B0"><span class="nav-text">1. 情况描述</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-%E6%96%B0%E5%BB%BA%E5%B9%B6%E5%88%87%E6%8D%A2%E5%88%86%E6%94%AF"><span class="nav-text">2. 新建并切换分支</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-%E6%8E%92%E6%9F%A5BUG%E5%B9%B6%E6%8F%90%E4%BA%A4"><span class="nav-text">3. 排查BUG并提交</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%B8%89%E3%80%81%E7%B4%A7%E6%80%A5%E4%BA%8B%E4%BB%B6"><span class="nav-text">三、紧急事件</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-%E6%83%85%E5%86%B5%E8%AF%B4%E6%98%8E"><span class="nav-text">1. 情况说明</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-%E5%88%87%E6%8D%A2%E5%9B%9Emaster%E5%88%86%E6%94%AF"><span class="nav-text">2. 切换回master分支</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-%E6%96%B0%E5%BB%BA%E5%B9%B6%E5%88%87%E6%8D%A2%E5%88%86%E6%94%AF"><span class="nav-text">3. 新建并切换分支</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-%E4%BF%AE%E5%A4%8D%E7%B4%A7%E6%80%A5%E9%97%AE%E9%A2%98%E5%B9%B6%E6%8F%90%E4%BA%A4"><span class="nav-text">4. 修复紧急问题并提交</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%9B%9B%E3%80%81%E5%88%86%E6%94%AF%E5%90%88%E5%B9%B6"><span class="nav-text">四、分支合并</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-%E6%83%85%E5%86%B5%E8%AF%B4%E6%98%8E-1"><span class="nav-text">1. 情况说明</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-git-merge"><span class="nav-text">2. git merge</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-%E5%88%87%E6%8D%A2%E5%88%B0master%E5%88%86%E6%94%AF"><span class="nav-text">3. 切换到master分支</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-%E5%90%88%E5%B9%B6hotfix%E5%88%86%E6%94%AF"><span class="nav-text">4. 合并hotfix分支</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%BA%94%E3%80%81%E7%BB%A7%E7%BB%ADBUG%E5%A4%84%E7%90%86"><span class="nav-text">五、继续BUG处理</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-%E5%88%87%E6%8D%A2%E5%88%B0iss53%E5%88%86%E6%94%AF"><span class="nav-text">1. 切换到iss53分支</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-%E4%BF%AE%E5%A4%8DBUG%E5%B9%B6%E6%8F%90%E4%BA%A4"><span class="nav-text">2. 修复BUG并提交</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-%E5%90%88%E5%B9%B6%E5%88%B0master%E5%88%86%E6%94%AF"><span class="nav-text">3. 合并到master分支</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-%E5%86%B2%E7%AA%81%E7%9A%84%E4%BA%A7%E7%94%9F%EF%BC%9F"><span class="nav-text">4. 冲突的产生？</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#5-%E8%A7%A3%E5%86%B3%E5%86%B2%E7%AA%81"><span class="nav-text">5. 解决冲突</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#6-%E6%8F%90%E4%BA%A4%E6%9B%B4%E6%96%B0"><span class="nav-text">6. 提交更新</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#7-%E5%90%88%E5%B9%B6%E5%88%86%E6%9E%90"><span class="nav-text">7. 合并分析</span></a></li></ol></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="苏木"
      src="/images/avatar.jpg">
  <p class="site-author-name" itemprop="name">苏木</p>
  <div class="site-description" itemprop="description">莫道桑榆晚，为霞尚满天</div>
</div>
<div class="site-state-wrap animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">673</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">42</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
        <span class="site-state-item-count">43</span>
        <span class="site-state-item-name">标签</span>
      </div>
  </nav>
</div>
  <div class="links-of-author animated">
      <span class="links-of-author-item">
        <a href="https://github.com/sumumm" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;sumumm" rel="noopener me" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
  </div>

        </div>
      </div>
    </div>

    
  </aside>


    </div>

    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://sumumm.github.io/post/6dd28b15.html">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.jpg">
      <meta itemprop="name" content="苏木">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="苏木">
      <meta itemprop="description" content="莫道桑榆晚，为霞尚满天">
    </span>

    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="LV01-Git-04-Git本地仓库-06-分支的新建与合并 | 苏木">
      <meta itemprop="description" content="">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          LV01-Git-04-Git本地仓库-06-分支的新建与合并
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2023-07-02 15:14:24" itemprop="dateCreated datePublished" datetime="2023-07-02T15:14:24+08:00">2023-07-02</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/" itemprop="url" rel="index"><span itemprop="name">开发工具</span></a>
        </span>
          ，
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/" itemprop="url" rel="index"><span itemprop="name">03版本控制</span></a>
        </span>
    </span>

  
    <span class="post-meta-break"></span>
    <span class="post-meta-item" title="本文字数">
      <span class="post-meta-item-icon">
        <i class="far fa-file-word"></i>
      </span>
      <span class="post-meta-item-text">本文字数：</span>
      <span>6.6k</span>
    </span>
    <span class="post-meta-item" title="阅读时长">
      <span class="post-meta-item-icon">
        <i class="far fa-clock"></i>
      </span>
      <span class="post-meta-item-text">阅读时长 &asymp;</span>
      <span>24 分钟</span>
    </span>
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody"><p>本文主要是Git中分支的新建与合并的相关笔记，若笔记中有错误或者不合适的地方，欢迎批评指正😃。</p>
<span id="more"></span>

<!-- Photo: https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-04-Git%E6%9C%AC%E5%9C%B0%E4%BB%93%E5%BA%93-06-%E5%88%86%E6%94%AF%E7%9A%84%E6%96%B0%E5%BB%BA%E4%B8%8E%E5%90%88%E5%B9%B6/img/ -->

<details class="folding-tag" blue><summary> 点击查看使用工具及版本 </summary>
              <div class='content'>
              <table>    <tr>        <td align="center" width=150px>Windows</td>        <td align="left">windows11</td>    </tr>    <tr>        <td align="center">Ubuntu</td>        <td align="left">Ubuntu16.04的64位版本</td>      </tr>    <tr>        <td align="center">VMware® Workstation 16 Pro</td>        <td align="left">16.2.3 build-19376536</td>      </tr></table>
              </div>
            </details>

<details class="folding-tag" blue><summary> 点击查看本文参考资料 </summary>
              <div class='content'>
              <table>    <tr><td align="center">参考方向  </td><td align="center">参考原文</td></tr>    <tr><td align="left">Git 官网</td><td align="left"><a href="https://git-scm.com/" target="_blank">https://git-scm.com/ <i class="fa fa-external-link-alt"></i> </a></td></tr>    <tr><td align="left">Git 官方文档</td><td align="left"><a href="https://git-scm.com/doc" target="_blank">https://git-scm.com/doc <i class="fa fa-external-link-alt"></i> </a></td></tr>    <tr><td align="left">Pro Git Book</td><td align="left"><a href="https://git-scm.com/book/zh/v2" target="_blank">https://git-scm.com/book/zh/v2 <i class="fa fa-external-link-alt"></i> </a></td></tr>    <tr><td align="left">Git 快速使用指南</td><td align="left"><a href="https://training.github.com/downloads/zh_CN/github-git-cheat-sheet/" target="_blank">https://training.github.com/downloads/zh_CN/github-git-cheat-sheet/ <i class="fa fa-external-link-alt"></i> </a></td></tr>    <tr><td align="left">Visual Git Cheat Sheet</td><td align="left"><a href="https://ndpsoftware.com/git-cheatsheet.html#loc=index" target="_blank">https://ndpsoftware.com/git-cheatsheet.html#loc=index <i class="fa fa-external-link-alt"></i> </a></td></tr></table>
              </div>
            </details>

<details class="folding-tag" blue><summary> 点击查看相关文件下载 </summary>
              <div class='content'>
              <table>    <tr>        <td align="center">---</td>        <td align="left">--- <a href="" target="_blank">  <i class="fa fa-external-link-alt"></i></a></td>      </tr></table>
              </div>
            </details>

<p>【说明】本节笔记的相关操作在Windows下进行，因为VS Code有个Git的插件，可以很直观的演示一些东西。由于Git安装后自带一个Git-Bash终端，所以就不用Win下的命令行啦，就用的这个终端，因为它里边的命令与Linux很类似，而windows中的命令行有些命令与linux并不相同，为了统一，还是用用Git自带的终端啦。另外VS Code是可以选择使用的终端的，我直接将VS Code使用的终端改成了git-bash，这样更方便一些。</p>
<h1 id="一、准备工作"><a href="#一、准备工作" class="headerlink" title="一、准备工作"></a><font size=3>一、准备工作</font></h1><h2 id="1-工作场景"><a href="#1-工作场景" class="headerlink" title="1. 工作场景"></a><font size=3>1. 工作场景</font></h2><p>让我们来看一个简单的分支新建与分支合并的例子，实际工作中可能会用到类似的工作流。 我们将经历如下步骤：</p>
<p>（1）用C语言开发某个项目。</p>
<p>（2）为实现某个新的用户需求，创建一个分支。</p>
<p>（3）在这个分支上开展工作。</p>
<p>突然发现，系统整体功能出现了BUG，我们需要在新的分支上修复这个BUG：</p>
<p>（1）新建修复BUG分支。</p>
<p>（2）在BUG分支修复BUG。</p>
<p>正在此时，突然接到一个电话说有个很严重的问题需要紧急修补。 我们将按照如下方式来处理：</p>
<p>（1）切换到我们的线上分支（production branch）。</p>
<p>（2）为这个紧急任务新建一个分支，并在其中修复它。</p>
<p>（3）在测试通过之后，切换回线上分支，然后合并这个修补分支，最后将改动推送到线上分支。</p>
<p>（4）切换回我们最初工作的分支上，继续工作。</p>
<h2 id="2-文件准备"><a href="#2-文件准备" class="headerlink" title="2. 文件准备"></a><font size=3>2. 文件准备</font></h2><p>我们需要先准备一个仓库，里边包含一些提交记录，我们提交三次，完成C0、C1和C2这三个功能的开发。</p>
<details class="folding-tag" blue><summary> 点击查看 main.c 文件内容 </summary>
              <div class='content'>
              <figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;unistd.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="comment">// C0功能实现</span></span><br><span class="line"><span class="type">void</span> <span class="title function_">func_C0</span><span class="params">(<span class="type">void</span>)</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;C0功能开始执行!\n&quot;</span>);</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;C0&gt;&gt;&gt;&gt;&gt;&gt;步骤1!\n&quot;</span>);</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;C0&gt;&gt;&gt;&gt;&gt;&gt;步骤2!\n&quot;</span>);</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;C0&gt;&gt;&gt;&gt;&gt;&gt;步骤3!\n&quot;</span>);</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;C0功能执行完毕!\n&quot;</span>);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// C1功能实现</span></span><br><span class="line"><span class="type">void</span> <span class="title function_">func_C1</span><span class="params">(<span class="type">void</span>)</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;C1功能开始执行!\n&quot;</span>);</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;C1&gt;&gt;&gt;&gt;&gt;&gt;步骤1!\n&quot;</span>);</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;C1&gt;&gt;&gt;&gt;&gt;&gt;步骤2!\n&quot;</span>);</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;C1&gt;&gt;&gt;&gt;&gt;&gt;步骤3!\n&quot;</span>);</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;C1功能执行完毕!\n&quot;</span>);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// C2功能实现</span></span><br><span class="line"><span class="type">void</span> <span class="title function_">func_C2</span><span class="params">(<span class="type">void</span>)</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;C2功能开始执行!\n&quot;</span>);</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;C2&gt;&gt;&gt;&gt;&gt;&gt;步骤1!\n&quot;</span>);</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;C2&gt;&gt;&gt;&gt;&gt;&gt;步骤2!\n&quot;</span>);</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;C2&gt;&gt;&gt;&gt;&gt;&gt;步骤3!\n&quot;</span>);</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;C2功能执行完毕!\n&quot;</span>);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">int</span> argc, <span class="type">const</span> <span class="type">char</span> *argv[])</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="comment">/* code */</span></span><br><span class="line">    <span class="keyword">while</span> (<span class="number">1</span>)</span><br><span class="line">    &#123;</span><br><span class="line">        func_C0(); <span class="comment">// 实现C0功能</span></span><br><span class="line">        func_C1(); <span class="comment">// 实现C1功能</span></span><br><span class="line">        func_C2(); <span class="comment">// 实现C2功能</span></span><br><span class="line">        sleep(<span class="number">1</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
              </div>
            </details>

<p>我们来看一下现在的提交记录：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-04-Git%E6%9C%AC%E5%9C%B0%E4%BB%93%E5%BA%93-06-%E5%88%86%E6%94%AF%E7%9A%84%E6%96%B0%E5%BB%BA%E4%B8%8E%E5%90%88%E5%B9%B6/img/image-20230623094425851.png" alt="image-20230623094425851" style="zoom: 67%;" />

<p>画一张简图就如下所示：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-04-Git%E6%9C%AC%E5%9C%B0%E4%BB%93%E5%BA%93-06-%E5%88%86%E6%94%AF%E7%9A%84%E6%96%B0%E5%BB%BA%E4%B8%8E%E5%90%88%E5%B9%B6/img/image-20230623094415782.png" alt="image-20230623094415782" style="zoom:50%;" />

<h1 id="二、BUG处理"><a href="#二、BUG处理" class="headerlink" title="二、BUG处理"></a><font size=3>二、BUG处理</font></h1><h2 id="1-情况描述"><a href="#1-情况描述" class="headerlink" title="1. 情况描述"></a><font size=3>1. 情况描述</font></h2><p>按照前边说的，这个时候需要有一个BUG出现，我们按照《Pro Git Book》中的例子，将这个BUG编号定为 #53 ，现在我们就要创建一个分支，然后在此分支上进行#53 BUG的修复。</p>
<h2 id="2-新建并切换分支"><a href="#2-新建并切换分支" class="headerlink" title="2. 新建并切换分支"></a><font size=3>2. 新建并切换分支</font></h2><p>前边我们知道通过以下命令可以新建分支，然后切换分支：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">git branch iss53</span><br><span class="line">git checkout iss53</span><br></pre></td></tr></table></figure>

<p>可是我们其实有更简单的方式，可以直接创建并切换到新建的分支，那就是带<code>-b</code>参数的<code>git checkout</code>命令：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">$ </span><span class="language-bash">git checkout -b iss53</span></span><br><span class="line">Switched to a new branch &#x27;iss53&#x27;</span><br></pre></td></tr></table></figure>

<p>它其实就等价于上边两条指令。现在我们的仓库情况如下图：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-04-Git%E6%9C%AC%E5%9C%B0%E4%BB%93%E5%BA%93-06-%E5%88%86%E6%94%AF%E7%9A%84%E6%96%B0%E5%BB%BA%E4%B8%8E%E5%90%88%E5%B9%B6/img/image-20230623094804875.png" alt="image-20230623094804875" style="zoom:50%;" />

<h2 id="3-排查BUG并提交"><a href="#3-排查BUG并提交" class="headerlink" title="3. 排查BUG并提交"></a><font size=3>3. 排查BUG并提交</font></h2><p>我们经过排查，功能C1上有问题，我们先修复C1的功能缺陷：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-04-Git%E6%9C%AC%E5%9C%B0%E4%BB%93%E5%BA%93-06-%E5%88%86%E6%94%AF%E7%9A%84%E6%96%B0%E5%BB%BA%E4%B8%8E%E5%90%88%E5%B9%B6/img/image-20230623094816938.png" alt="image-20230623094816938" style="zoom:50%;" />

<p>然后我们进行一次提交：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git commit -a -m  &quot;feat:C3 修复C1出现的BUG[issue 53]&quot;</span><br></pre></td></tr></table></figure>

<p>当前仓库的情况如下：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-04-Git%E6%9C%AC%E5%9C%B0%E4%BB%93%E5%BA%93-06-%E5%88%86%E6%94%AF%E7%9A%84%E6%96%B0%E5%BB%BA%E4%B8%8E%E5%90%88%E5%B9%B6/img/image-20230623095643939.png" alt="image-20230623095643939" style="zoom:50%;" />

<p>但是这个BUG似乎没有完全解决，我们还要继续再排查，按照剧情需要，此时需要有紧急事件出现。</p>
<h1 id="三、紧急事件"><a href="#三、紧急事件" class="headerlink" title="三、紧急事件"></a><font size=3>三、紧急事件</font></h1><p>剧情发展需要，果然出现了紧急事件。</p>
<h2 id="1-情况说明"><a href="#1-情况说明" class="headerlink" title="1. 情况说明"></a><font size=3>1. 情况说明</font></h2><p>现在我们接到那个电话，有个紧急问题等待我们来解决，我们需要在C2功能的基础上再开发一个C4功能。 有了 Git 的帮助，我们不必把这个紧急问题和 <code>iss53</code> 的修改混在一起， 也不需要花大力气来还原关于 #53 问题的修改，然后再添加关于这个紧急问题的修改，最后将这个修改提交到线上分支。 我们所要做的仅仅是切换回 <code>master</code> 分支，然后新建分支，处理紧急事件，BUG分支就可以先扔一边。</p>
<h2 id="2-切换回master分支"><a href="#2-切换回master分支" class="headerlink" title="2. 切换回master分支"></a><font size=3>2. 切换回master分支</font></h2><p>在这么做之前，要留意工作目录和暂存区里那些还没有被提交的修改， 它可能会和我们即将检出的分支产生冲突从而阻止 Git 切换到该分支。 最好的方法是，在我们切换分支之前，保持好一个干净的状态。 有一些方法可以绕过这个问题（即，暂存（stashing） 和 修补提交（commit amending））， 会在 <a target="_blank" rel="noopener" href="https://git-scm.com/book/zh/v2/ch00/_git_stashing">贮藏与清理</a> 中看到关于这两个命令的介绍。 现在，我们假设已经把你的修改全部提交了，这时可以切换回 <code>master</code> 分支了：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">$ </span><span class="language-bash">git checkout master</span></span><br><span class="line">Switched to branch &#x27;master&#x27;</span><br></pre></td></tr></table></figure>

<p>这个时候，我们的工作目录和我们在开始 #53 问题之前一模一样，现在可以专心修复紧急问题了。 当我们切换分支的时候，Git 会重置我们的工作目录，使其看起来像回到了我们在那个分支上最后一次提交的样子。 Git 会自动添加、删除、修改文件以确保此时你的工作目录和这个分支最后一次提交时的样子一模一样。</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-04-Git%E6%9C%AC%E5%9C%B0%E4%BB%93%E5%BA%93-06-%E5%88%86%E6%94%AF%E7%9A%84%E6%96%B0%E5%BB%BA%E4%B8%8E%E5%90%88%E5%B9%B6/img/image-20230623095708428.png" alt="image-20230623095708428" style="zoom:50%;" />

<h2 id="3-新建并切换分支"><a href="#3-新建并切换分支" class="headerlink" title="3. 新建并切换分支"></a><font size=3>3. 新建并切换分支</font></h2><p>接下来，要修复这个紧急问题。 我们来建立一个 <code>hotfix</code> 分支，在该分支上工作直到问题解决：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">$ </span><span class="language-bash">git checkout -b hotfix</span></span><br><span class="line">Switched to a new branch &#x27;hotfix&#x27;</span><br></pre></td></tr></table></figure>

<p>现在仓库的情况如下：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-04-Git%E6%9C%AC%E5%9C%B0%E4%BB%93%E5%BA%93-06-%E5%88%86%E6%94%AF%E7%9A%84%E6%96%B0%E5%BB%BA%E4%B8%8E%E5%90%88%E5%B9%B6/img/image-20230623100350794.png" alt="image-20230623100350794" style="zoom:50%;" />

<h2 id="4-修复紧急问题并提交"><a href="#4-修复紧急问题并提交" class="headerlink" title="4. 修复紧急问题并提交"></a><font size=3>4. 修复紧急问题并提交</font></h2><p>我们上一步已经切换到hotfix分支，排查后发现这个问题出现在C2功能上：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-04-Git%E6%9C%AC%E5%9C%B0%E4%BB%93%E5%BA%93-06-%E5%88%86%E6%94%AF%E7%9A%84%E6%96%B0%E5%BB%BA%E4%B8%8E%E5%90%88%E5%B9%B6/img/image-20230623100607272.png" alt="image-20230623100607272" style="zoom:50%;" />

<p>我们处理完紧急问题后，进行一次提交：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git commit -a -m &quot;feat:C4 修复紧急问题&quot;</span><br></pre></td></tr></table></figure>

<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-04-Git%E6%9C%AC%E5%9C%B0%E4%BB%93%E5%BA%93-06-%E5%88%86%E6%94%AF%E7%9A%84%E6%96%B0%E5%BB%BA%E4%B8%8E%E5%90%88%E5%B9%B6/img/image-20230623100836073.png" alt="image-20230623100836073" style="zoom:50%;" />

<p>当前分支的提交记录如下：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-04-Git%E6%9C%AC%E5%9C%B0%E4%BB%93%E5%BA%93-06-%E5%88%86%E6%94%AF%E7%9A%84%E6%96%B0%E5%BB%BA%E4%B8%8E%E5%90%88%E5%B9%B6/img/image-20230623100916660.png" alt="image-20230623100916660" style="zoom:67%;" />

<h1 id="四、分支合并"><a href="#四、分支合并" class="headerlink" title="四、分支合并"></a><font size=3>四、分支合并</font></h1><h2 id="1-情况说明-1"><a href="#1-情况说明-1" class="headerlink" title="1. 情况说明"></a><font size=3>1. 情况说明</font></h2><p>我们上边已经修复了紧急问题，那么现在需要将修复紧急问题后的代码合并到master分支。</p>
<h2 id="2-git-merge"><a href="#2-git-merge" class="headerlink" title="2. git merge"></a><font size=3>2. git merge</font></h2><p>我们可以通过下边的命令来完成分支的合并：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git merge branch_name</span><br></pre></td></tr></table></figure>

<p>branch_name为需要合并到当前分支上的分支名。需要注意的是，我们要合并分支，需要先切换到目标分支去，例如这里，我们想要把hotfix分支的内容合并到mater分支，我们就需要先切换到mater分支。</p>
<h2 id="3-切换到master分支"><a href="#3-切换到master分支" class="headerlink" title="3. 切换到master分支"></a><font size=3>3. 切换到master分支</font></h2><p>我们将hotfix分支的内容都提交后，就可以切换到master分支了：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git checkout master</span><br></pre></td></tr></table></figure>

<p>我们查看一下提交记录们就会发现master分支根本没有变化，还是最开始的三次提交。</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-04-Git%E6%9C%AC%E5%9C%B0%E4%BB%93%E5%BA%93-06-%E5%88%86%E6%94%AF%E7%9A%84%E6%96%B0%E5%BB%BA%E4%B8%8E%E5%90%88%E5%B9%B6/img/image-20230623101753441.png" alt="image-20230623101753441" style="zoom:67%;" />

<p>当前仓库的情况如下：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-04-Git%E6%9C%AC%E5%9C%B0%E4%BB%93%E5%BA%93-06-%E5%88%86%E6%94%AF%E7%9A%84%E6%96%B0%E5%BB%BA%E4%B8%8E%E5%90%88%E5%B9%B6/img/image-20230623101848979.png" alt="image-20230623101848979" style="zoom:50%;" />

<h2 id="4-合并hotfix分支"><a href="#4-合并hotfix分支" class="headerlink" title="4. 合并hotfix分支"></a><font size=3>4. 合并hotfix分支</font></h2><p>我们执行以下命令：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">$ </span><span class="language-bash">git merge hotfix</span></span><br><span class="line">Updating 08ec435..f5a3adf</span><br><span class="line">Fast-forward</span><br><span class="line"> main.c | 5 +++++</span><br><span class="line"> 1 file changed, 5 insertions(+)</span><br></pre></td></tr></table></figure>

<p>在合并的时候，我们应该注意到了“快进（fast-forward）”这个词。 由于我们想要合并的分支 <code>hotfix</code> 所指向的提交 <code>C4</code> 是我们所在的提交 <code>C2</code> 的<strong>直接后继</strong>， 因此 Git 会直接将<strong>指针向前移动</strong>。换句话说，当我们试图合并两个分支时， 如果顺着一个分支走下去能够到达另一个分支，那么 Git 在合并两者的时候， 只会简单的将指针向前推进（指针右移），因为这种情况下的合并操作没有需要解决的分歧——这就叫做 “快进（fast-forward）”。当前仓库的情况如下图：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-04-Git%E6%9C%AC%E5%9C%B0%E4%BB%93%E5%BA%93-06-%E5%88%86%E6%94%AF%E7%9A%84%E6%96%B0%E5%BB%BA%E4%B8%8E%E5%90%88%E5%B9%B6/img/image-20230623102418086.png" alt="image-20230623102418086" style="zoom:50%;" />

<p>然后我们来看一下提交记录：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-04-Git%E6%9C%AC%E5%9C%B0%E4%BB%93%E5%BA%93-06-%E5%88%86%E6%94%AF%E7%9A%84%E6%96%B0%E5%BB%BA%E4%B8%8E%E5%90%88%E5%B9%B6/img/image-20230623102025200.png" alt="image-20230623102025200" style="zoom:67%;" />

<p>发现C4的修复已经合并到master这里来了，我们看一下main.c的func_C2函数：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-04-Git%E6%9C%AC%E5%9C%B0%E4%BB%93%E5%BA%93-06-%E5%88%86%E6%94%AF%E7%9A%84%E6%96%B0%E5%BB%BA%E4%B8%8E%E5%90%88%E5%B9%B6/img/image-20230623102127913.png" alt="image-20230623102127913" style="zoom:50%;" />

<p>会发现，对应的紧急问题已经被修复了。</p>
<h1 id="五、继续BUG处理"><a href="#五、继续BUG处理" class="headerlink" title="五、继续BUG处理"></a><font size=3>五、继续BUG处理</font></h1><p>上边的紧急情况处理完了，但是我们的BUG还没有完全解决掉，只是修复了一小部分，我们还需要继续排查。</p>
<h2 id="1-切换到iss53分支"><a href="#1-切换到iss53分支" class="headerlink" title="1. 切换到iss53分支"></a><font size=3>1. 切换到iss53分支</font></h2><p>我们切换到iss53分支继续进行BUG的修复：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">$ </span><span class="language-bash">git checkout iss53</span></span><br><span class="line">Switched to branch &#x27;iss53&#x27;</span><br></pre></td></tr></table></figure>

<p>此时的仓库情况如下：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-04-Git%E6%9C%AC%E5%9C%B0%E4%BB%93%E5%BA%93-06-%E5%88%86%E6%94%AF%E7%9A%84%E6%96%B0%E5%BB%BA%E4%B8%8E%E5%90%88%E5%B9%B6/img/image-20230623102951487.png" alt="image-20230623102951487" style="zoom:50%;" />

<p>还可以看一下当前的提交记录：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-04-Git%E6%9C%AC%E5%9C%B0%E4%BB%93%E5%BA%93-06-%E5%88%86%E6%94%AF%E7%9A%84%E6%96%B0%E5%BB%BA%E4%B8%8E%E5%90%88%E5%B9%B6/img/image-20230623103021721.png" alt="image-20230623103021721" style="zoom:67%;" />

<p>可以看到此分支还停留在修复部分#53 BUG的时候，并没有受到任何影响。我们在 <code>hotfix</code> 分支上所做的工作并没有包含到 <code>iss53</code> 分支中。 如果我们需要拉取 <code>hotfix</code> 所做的修改，可以使用 <code>git merge master</code> 命令将 <code>master</code> 分支合并入 <code>iss53</code> 分支，或者也可以等到 <code>iss53</code> 分支完成其使命，再将其合并回 <code>master</code> 分支。</p>
<h2 id="2-修复BUG并提交"><a href="#2-修复BUG并提交" class="headerlink" title="2. 修复BUG并提交"></a><font size=3>2. 修复BUG并提交</font></h2><p>这一次我们将BUG完全修复，本次修复我们更改了以下内容：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-04-Git%E6%9C%AC%E5%9C%B0%E4%BB%93%E5%BA%93-06-%E5%88%86%E6%94%AF%E7%9A%84%E6%96%B0%E5%BB%BA%E4%B8%8E%E5%90%88%E5%B9%B6/img/image-20230623103542761.png" alt="image-20230623103542761" style="zoom: 50%;" />

<p>然后我们进行一次提交：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git commit -a -m &quot;feat:C5 结束BUG的修复[issue 53]&quot;</span><br></pre></td></tr></table></figure>

<p>此时，iss53分支提交记录如下：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-04-Git%E6%9C%AC%E5%9C%B0%E4%BB%93%E5%BA%93-06-%E5%88%86%E6%94%AF%E7%9A%84%E6%96%B0%E5%BB%BA%E4%B8%8E%E5%90%88%E5%B9%B6/img/image-20230623103725644.png" alt="image-20230623103725644" style="zoom:67%;" />

<p>整个仓库的情况如下：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-04-Git%E6%9C%AC%E5%9C%B0%E4%BB%93%E5%BA%93-06-%E5%88%86%E6%94%AF%E7%9A%84%E6%96%B0%E5%BB%BA%E4%B8%8E%E5%90%88%E5%B9%B6/img/image-20230623103911519.png" alt="image-20230623103911519" style="zoom:50%;" />

<h2 id="3-合并到master分支"><a href="#3-合并到master分支" class="headerlink" title="3. 合并到master分支"></a><font size=3>3. 合并到master分支</font></h2><p>我们已经修正了 #53 问题，并且打算将我们的工作合并入 <code>master</code> 分支。 为此，我们需要合并 <code>iss53</code> 分支到 <code>master</code> 分支，这和之前合并 <code>hotfix</code> 分支所做的工作差不多。 我们只需要切换到想合并入的分支，然后运行 <code>git merge</code> 命令：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">$ </span><span class="language-bash">git checkout master</span></span><br><span class="line">Switched to branch &#x27;master&#x27;</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_">$ </span><span class="language-bash"> git merge iss53</span></span><br><span class="line">Auto-merging main.c</span><br><span class="line">CONFLICT (content): Merge conflict in main.c</span><br><span class="line">Automatic merge failed; fix conflicts and then commit the result.</span><br></pre></td></tr></table></figure>

<p>发生了什么？我们看到了一个报错：<code>Automatic merge failed</code>，我们先来看一下master的提交记录：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-04-Git%E6%9C%AC%E5%9C%B0%E4%BB%93%E5%BA%93-06-%E5%88%86%E6%94%AF%E7%9A%84%E6%96%B0%E5%BB%BA%E4%B8%8E%E5%90%88%E5%B9%B6/img/image-20230623104336434.png" alt="image-20230623104336434" style="zoom:67%;" />

<p>我们发现，master分支并没有产生新的提交记录，我们再看一下文件内容：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-04-Git%E6%9C%AC%E5%9C%B0%E4%BB%93%E5%BA%93-06-%E5%88%86%E6%94%AF%E7%9A%84%E6%96%B0%E5%BB%BA%E4%B8%8E%E5%90%88%E5%B9%B6/img/image-20230623104449722.png" alt="image-20230623104449722" style="zoom: 41%;" />

<p>我们发现这里出现了这么一段内容，这就叫做<strong>冲突</strong>。</p>
<h2 id="4-冲突的产生？"><a href="#4-冲突的产生？" class="headerlink" title="4. 冲突的产生？"></a><font size=3>4. 冲突的产生？</font></h2><p>有时候合并操作不会很顺利。 如果在两个不同的分支中，对同一个文件的同一个部分进行了不同的修改，Git 就没法干净的合并它们。 如果我们对 #53 问题的修改和有关 <code>hotfix</code> 分支的修改都涉及到同一个文件的同一处，在合并它们的时候就会产生合并冲突：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">$ </span><span class="language-bash"> git merge iss53</span></span><br><span class="line">Auto-merging main.c</span><br><span class="line">CONFLICT (content): Merge conflict in main.c</span><br><span class="line">Automatic merge failed; fix conflicts and then commit the result.</span><br></pre></td></tr></table></figure>

<p>此时 Git 做了合并，但是没有自动地创建一个新的合并提交。 Git 会暂停下来，等待我们去解决合并产生的冲突。 我们可以在合并冲突后的任意时刻使用 <code>git status</code> 命令来查看那些因包含合并冲突而处于未合并（unmerged）状态的文件：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-04-Git%E6%9C%AC%E5%9C%B0%E4%BB%93%E5%BA%93-06-%E5%88%86%E6%94%AF%E7%9A%84%E6%96%B0%E5%BB%BA%E4%B8%8E%E5%90%88%E5%B9%B6/img/image-20230623104901587.png" alt="image-20230623104901587" style="zoom:67%;" />

<p>任何因包含合并冲突而有待解决的文件，都会以未合并状态标识出来。 Git 会在有冲突的文件中加入标准的冲突解决标记，这样我们可以打开这些包含冲突的文件然后手动解决冲突。 出现冲突的文件会包含一些特殊区段，看起来像下面这个样子：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-04-Git%E6%9C%AC%E5%9C%B0%E4%BB%93%E5%BA%93-06-%E5%88%86%E6%94%AF%E7%9A%84%E6%96%B0%E5%BB%BA%E4%B8%8E%E5%90%88%E5%B9%B6/img/image-20230623104955077.png" alt="image-20230623104955077" style="zoom:65%;" />

<p>这表示 <code>HEAD</code> 所指示的版本（也就是我们的 <code>master</code> 分支所在的位置，因为你在运行 merge 命令的时候已经检出到了这个分支）在这个区段的上半部分（<code>=======</code> 的上半部分），而 <code>iss53</code> 分支所指示的版本在 <code>=======</code> 的下半部分。 为了解决冲突，我们必须选择使用由 <code>=======</code> 分割的两部分中的一个，或者我们也可以自行合并这些内容。 我们先对比一下合并了hotfix分支的master分支、hotfix分支和iss53分支的main.c这几行：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-04-Git%E6%9C%AC%E5%9C%B0%E4%BB%93%E5%BA%93-06-%E5%88%86%E6%94%AF%E7%9A%84%E6%96%B0%E5%BB%BA%E4%B8%8E%E5%90%88%E5%B9%B6/img/image-20230623110250694.png" alt="image-20230623110250694" style="zoom:33%;" />

<p>发现同一个文件同一行，两个分支中的内容是不一样的，这样合并的时候，Git无法自动判定要使用哪一个分支的内容，于是就产生了冲突，需要我们自己手动解决。</p>
<h2 id="5-解决冲突"><a href="#5-解决冲突" class="headerlink" title="5. 解决冲突"></a><font size=3>5. 解决冲突</font></h2><p>我们既要修复紧急问题，也要完成BUG修复，所以两者都要，我们可以这样处理冲突：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">// C2功能实现</span><br><span class="line">void func_C2(void)</span><br><span class="line">&#123;</span><br><span class="line">    printf(&quot;C2功能开始执行!\n&quot;);</span><br><span class="line">    printf(&quot;#53 继续BUG修复添加内容1!\n&quot;);</span><br><span class="line">    printf(&quot;#53 继续BUG修复添加内容2!\n&quot;);</span><br><span class="line">    printf(&quot;C2&gt;&gt;&gt;&gt;&gt;&gt;步骤1!\n&quot;);</span><br><span class="line">    printf(&quot;C4&gt;&gt;&gt;&gt;&gt;紧急问题处理1!\n&quot;);</span><br><span class="line">    printf(&quot;C4&gt;&gt;&gt;&gt;&gt;紧急问题处理2!\n&quot;);</span><br><span class="line">    printf(&quot;C4&gt;&gt;&gt;&gt;&gt;紧急问题处理3!\n&quot;);</span><br><span class="line">    printf(&quot;C4&gt;&gt;&gt;&gt;&gt;紧急问题处理4!\n&quot;);</span><br><span class="line">    printf(&quot;C4&gt;&gt;&gt;&gt;&gt;紧急问题处理5!\n&quot;);</span><br><span class="line">    printf(&quot;#53 继续BUG修复添加内容3!\n&quot;);</span><br><span class="line">    printf(&quot;C2&gt;&gt;&gt;&gt;&gt;&gt;步骤2!\n&quot;);</span><br><span class="line">    printf(&quot;C2&gt;&gt;&gt;&gt;&gt;&gt;步骤3!\n&quot;);</span><br><span class="line">    printf(&quot;C2功能执行完毕!\n&quot;);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>直接将多余的删掉即可。</p>
<h2 id="6-提交更新"><a href="#6-提交更新" class="headerlink" title="6. 提交更新"></a><font size=3>6. 提交更新</font></h2><p>在我们解决了所有文件里的冲突之后，对每个文件使用 <code>git add</code> 命令来将其标记为冲突已解决。 <strong>一旦暂存这些原本有冲突的文件，Git 就会将它们标记为冲突已解决</strong>，即便我们没有处理冲突。</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">git add .</span><br><span class="line">git status</span><br></pre></td></tr></table></figure>



<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-04-Git%E6%9C%AC%E5%9C%B0%E4%BB%93%E5%BA%93-06-%E5%88%86%E6%94%AF%E7%9A%84%E6%96%B0%E5%BB%BA%E4%B8%8E%E5%90%88%E5%B9%B6/img/image-20230623110814490.png" alt="image-20230623110814490" style="zoom:67%;" />

<p>然后我们提交此次合并：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git commit -m &quot;feat:C6 合并iss53(冲突已解决)&quot;</span><br></pre></td></tr></table></figure>

<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-04-Git%E6%9C%AC%E5%9C%B0%E4%BB%93%E5%BA%93-06-%E5%88%86%E6%94%AF%E7%9A%84%E6%96%B0%E5%BB%BA%E4%B8%8E%E5%90%88%E5%B9%B6/img/image-20230623111340721.png" alt="image-20230623111340721" style="zoom:67%;" />

<p>可以看到master分支多出了两次提交，这两次就是iss53中的处理BUG的修改，最新的一次是我们在mater进行的提交，是进行的冲突解决。</p>
<h2 id="7-合并分析"><a href="#7-合并分析" class="headerlink" title="7. 合并分析"></a><font size=3>7. 合并分析</font></h2><p>这和我们在《四、分支合并》这一节笔记中合并 <code>hotfix</code> 分支的时候看起来有一点不一样。 在这种情况下，我们的开发历史从一个更早的地方开始分叉开来（diverged）。 因为，<code>master</code> 分支所在提交并不是 <code>iss53</code> 分支所在提交的直接祖先，Git 不得不做一些额外的工作。 出现这种情况的时候，Git 会使用两个分支的末端所指的快照（<code>C4</code> 和 <code>C5</code>）以及这两个分支的公共祖先（<code>C2</code>），做一个简单的三方合并。</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-04-Git%E6%9C%AC%E5%9C%B0%E4%BB%93%E5%BA%93-06-%E5%88%86%E6%94%AF%E7%9A%84%E6%96%B0%E5%BB%BA%E4%B8%8E%E5%90%88%E5%B9%B6/img/image-20230623112122799.png" alt="image-20230623112122799" style="zoom:50%;" />

<p>和之前将分支指针向前推进所不同的是，Git 将此次三方合并的结果做了一个新的快照并且自动创建一个新的提交指向它。 这个被称作一次合并提交，它的特别之处在于他有不止一个父提交。</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-04-Git%E6%9C%AC%E5%9C%B0%E4%BB%93%E5%BA%93-06-%E5%88%86%E6%94%AF%E7%9A%84%E6%96%B0%E5%BB%BA%E4%B8%8E%E5%90%88%E5%B9%B6/img/image-20230623112318972.png" alt="image-20230623112318972" style="zoom:50%;" />

    </div>

    
    
    

    <footer class="post-footer">




    <div>
        
            <div style="text-align:center;color: #ccc;font-size:14px;">
            ----------本文结束
            <i class="fas fa-fan fa-spin" style="color: #FF1493; font-size: 1rem"></i>
            感谢您的阅读----------
            </div>
        
    </div>





  
  <div class="my_post_copyright"> 
    <p><span>文章标题:</span><a href="/post/6dd28b15.html">LV01-Git-04-Git本地仓库-06-分支的新建与合并</a></p>
    <p><span>文章作者:</span><a href="/" title="欢迎访问 《苏木》 的学习笔记">苏木</a></p>
    <p><span>发布时间:</span>2023年07月02日 - 15:14</p>
    <p><span>最后更新:</span>2025年06月14日 - 00:25</p>
    <p><span>原始链接:</span><a href="/post/6dd28b15.html" title="LV01-Git-04-Git本地仓库-06-分支的新建与合并">https://sumumm.github.io/post/6dd28b15.html</a></p>
    <p><span>许可协议:</span><i class="fab fa-creative-commons"></i> <a rel="license" href= "https://creativecommons.org/licenses/by-nc-nd/4.0/" target="_blank" title="Attribution-NonCommercial-NoDerivatives 4.0 International (CC BY-NC-ND 4.0)">署名-非商业性使用-禁止演绎 4.0 国际</a> 转载请保留原文链接及作者。</p>  
  </div>
  


          <div class="post-tags">
              <a href="/tags/Git/" rel="tag"><i class="fa fa-tag"></i> Git</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/post/4540c63.html" rel="prev" title="LV01-Git-04-Git本地仓库-07-分支管理">
                  <i class="fa fa-angle-left"></i> LV01-Git-04-Git本地仓库-07-分支管理
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/post/fc417449.html" rel="next" title="LV01-Git-04-Git本地仓库-05-Git分支简介">
                  LV01-Git-04-Git本地仓库-05-Git分支简介 <i class="fa fa-angle-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">

  <div class="copyright">
    &copy; 2017 – 
    <span itemprop="copyrightYear">2025</span>
    <span class="with-love">
      <i class="fa fa-heart"></i>
    </span>
    <span class="author" itemprop="copyrightHolder">苏木</span>
  </div>
<div class="wordcount">
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-line"></i>
    </span>
      <span>站点总字数：</span>
    <span title="站点总字数">3.7m</span>
  </span>
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
      <span>站点阅读时长 &asymp;</span>
    <span title="站点阅读时长">225:26</span>
  </span>
</div>




    <span id="sitetime"></span>
    <script defer language=javascript>
        function siteTime()
        {
            window.setTimeout("siteTime()", 1000);
            var seconds = 1000;
            var minutes = seconds * 60;
            var hours = minutes * 60;
            var days = hours * 24;
            var years = days * 365;
            var today = new Date();
            var todayYear = today.getFullYear();
            var todayMonth = today.getMonth()+1;
            var todayDate = today.getDate();
            var todayHour = today.getHours();
            var todayMinute = today.getMinutes();
            var todaySecond = today.getSeconds();
            /*==================================================
            Date.UTC() -- 返回date对象距世界标准时间(UTC)1970年1月1日午夜之间的毫秒数(时间戳)
            year        - 作为date对象的年份，为4位年份值
            month       - 0-11之间的整数，做为date对象的月份
            day         - 1-31之间的整数，做为date对象的天数
            hours       - 0(午夜24点)-23之间的整数，做为date对象的小时数
            minutes     - 0-59之间的整数，做为date对象的分钟数
            seconds     - 0-59之间的整数，做为date对象的秒数
            microseconds - 0-999之间的整数，做为date对象的毫秒数
            ==================================================*/
            var t1 = Date.UTC(2017, 
                              5, 
                              19, 
                              0, 
                              0, 
                              0); //北京时间
            var t2 = Date.UTC(todayYear,todayMonth,todayDate,todayHour,todayMinute,todaySecond);
            var diff = t2-t1;
            var diffYears = Math.floor(diff/years);
            var diffDays = Math.floor((diff/days)-diffYears*365);
            var diffHours = Math.floor((diff-(diffYears*365+diffDays)*days)/hours);
            var diffMinutes = Math.floor((diff-(diffYears*365+diffDays)*days-diffHours*hours)/minutes);
            var diffSeconds = Math.floor((diff-(diffYears*365+diffDays)*days-diffHours*hours-diffMinutes*minutes)/seconds);
            document.getElementById("sitetime").innerHTML="已在这里 "+diffYears+" 年 "+diffDays+" 天 "+diffHours+" 小时 "+diffMinutes+" 分钟 "+diffSeconds+" 秒";
        }
        siteTime();
    </script>



    </div>
  </footer>

  
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up fa-lg"></i>
    <span>0%</span>
  </div>
  <div class="reading-progress-bar"></div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


  
  <script src="https://cdnjs.cloudflare.com/ajax/libs/animejs/3.2.1/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/next-theme-pjax/0.6.0/pjax.min.js" integrity="sha256-vxLn1tSKWD4dqbMRyv940UYw4sXgMtYcK6reefzZrao=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/fancyapps-ui/5.0.28/fancybox/fancybox.umd.js" integrity="sha256-ytMJGN3toR+a84u7g7NuHm91VIR06Q41kMWDr2pq7Zo=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/lozad.js/1.16.0/lozad.min.js" integrity="sha256-mOFREFhqmHeQbXpK2lp4nA3qooVgACfh88fpJftLBbc=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/next-boot.js"></script><script src="/js/pjax.js"></script>

  <script src="https://cdnjs.cloudflare.com/ajax/libs/hexo-generator-searchdb/1.4.1/search.js" integrity="sha256-1kfA5uHPf65M5cphT2dvymhkuyHPQp5A53EGZOnOLmc=" crossorigin="anonymous"></script>
<script src="/js/third-party/search/local-search.js"></script>




  <script src="/js/third-party/fancybox.js"></script>

  <script src="/js/third-party/pace.js"></script>


  




  

  <script class="next-config" data-name="enableMath" type="application/json">false</script><script class="next-config" data-name="mathjax" type="application/json">{"enable":true,"tags":"none","js":{"url":"https://cdnjs.cloudflare.com/ajax/libs/mathjax/3.2.2/es5/tex-mml-chtml.js","integrity":"sha256-MASABpB4tYktI2Oitl4t+78w/lyA+D7b/s9GEP0JOGI="}}</script>
<script src="/js/third-party/math/mathjax.js"></script>


 
        <div id="click-show-text"
            data-mobile = false
            data-text = 富强,民主,文明,和谐,自由,平等,公正,法制,爱国,敬业,诚信,友善
            data-fontsize = 15px
            data-random= false>
        </div>
       

      
        <script async src=https://cdn.jsdelivr.net/npm/hexo-next-mouse-effect@latest/click/showText.js></script>
      

      
    




    <script async src="/js/fancybox_param.js"></script>





<!-- APlayer本体 -->



</body>
</html>
